package com.example.controller;

import com.example.dto.Response;
import com.example.model.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String index(){
        return "login";
    }

    @RequestMapping("/auth.html")
    @ResponseBody
    public Response auth(HttpServletRequest request, @RequestParam String userName, @RequestParam String password){
        User user = userService.getUserByUsernameAndPasswd(userName, password);
        if(user != null){
            request.getSession().setAttribute("SESSION_USER", user);
            return new Response().returnSuccess();
        }
        return new Response().returnFailure();
    }

    @RequestMapping("/main.html")
    public String main() throws Exception{
        return "main";
    }
}
